package stock.commons;

import java.sql.ResultSet;

import stock.database.DBValuesSetter;
import stock.database.GetSelect;

import com.mysql.jdbc.Connection;

public class DBLoader {
	public void updateStock(Connection conn) {
		GetSelect gs = new GetSelect();
		DBValuesSetter DBVSUpdateStock = new DBValuesSetter();


		try {
			ResultSet rs = gs.getStockConsolidado(conn);
			while (rs.next()) {
				DBVSUpdateStock.insert("insert into stock values (\""
						+ rs.getString(1).replace("\"", "\\\"") + "\",\""
						+ rs.getString(2).replace("\"", "\\\"") + "\","
						+ rs.getInt(3) + ")",
						conn);
			}
		} catch (Exception e) {
			System.out.println("el error ocurrio en updateStock");
			e.printStackTrace();
		}
	}

	public void updateMovimientos(Connection conn) {
		GetSelect gs = new GetSelect();
		DBValuesSetter DBVSUpdateStock = new DBValuesSetter();
		try {
			ResultSet rs = gs.getMovimientosConsolidado(conn);
			while (rs.next()) {

				DBVSUpdateStock.insert("insert into movimientos values (\""
						+ rs.getString(1).replace("\"", "\\\"") + "\",\""
						+ rs.getString(2).replace("\"", "\\\"") + "\","
						+ rs.getInt(3) + ")",
						conn);
			}
		} catch (Exception e) {
			System.out.println("el error ocurrio en updateStock");
			e.printStackTrace();
		}
	}

	public void updateFinalReport(Connection conn) {
		GetSelect gs = new GetSelect();
		DBValuesSetter DBVSUpdateStock = new DBValuesSetter();

		try {
			ResultSet rs = gs.getMovimientosAndStock(conn);
			while (rs.next()) {
				String id = rs.getString(1).replace("\"", "\\\"");
				String descripcion = rs.getString(2).replace("\"", "\\\"");
				int stock = rs.getInt(3);
				int movimientos = rs.getInt(4);
				movimientos = movimientos / 6;
				int stockfuturo = stock - (movimientos * 3);
				DBVSUpdateStock.insert("insert into reportefinal values (\"" + id
						+ "\",\"" + descripcion + "\"," + stock + ","
						+ movimientos + "," + stockfuturo + ")", conn);
			}
		} catch (Exception e) {
			System.out.println("el error ocurrio en updateStock");
			e.printStackTrace();
		}
	}

	public void deleteValuesToUploadFile(Connection conn) {
		DBValuesSetter DBVSdelete = new DBValuesSetter();
		try {
			DBVSdelete.delete("delete from jabbar", conn);
			DBVSdelete.delete("delete from bird", conn);
			DBVSdelete.delete("delete from jabbar2", conn);
			DBVSdelete.delete("delete from bird2", conn);
			DBVSdelete.delete("delete from stock", conn);
			DBVSdelete.delete("delete from movimientos", conn);
			DBVSdelete.delete("delete from reportefinal", conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
